package game.sound
{
	import engine.asset.*;
	import engine.core.*;
	import engine.sound.*;
	import game.*;
	import game.actor.*;
	import game.core.*;
	
	public class RxMusicManager
	{
		private static const STATE_IDLE:int = 0;
		private static const STATE_MAP:int = 1;
		private static const STATE_COMBAT:int = 2;
		private static var s_state:int;
		private static var s_musicLoaded:Boolean = false;
		private static var s_musicVolume:Number;
		private static var s_stingerVolume:Number;
		private static var s_mapVolume:Number;
		private static var s_ambientVolume:Number;
		private static var s_combatVolume:Number;
		private static var s_stingerChannel:RxSoundChannel;
		private static var s_mapChannel:RxSoundChannel;
		private static var s_ambientChannel:RxSoundChannel;
		private static var s_combatChannel:RxSoundChannel;
		private static var s_musicEnabled:Boolean = true;
		
		public function RxMusicManager()
		{
		}
		
		public static function Update() : void
		{
			if (!s_musicEnabled)
			{
				return;
			}
			switch(s_state)
			{
				case STATE_IDLE:
				case STATE_COMBAT:
				{
					break;
				}
				case STATE_MAP:
				{
					if (_isInCombat())
					{
						_playCombatMusic();
					}
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		private static function _isInCombat() : Boolean
		{
			
			var _loc_2:RxActor = null;
			var _loc_1:* = RxEntity.FindByType(RxEntityType.ENTITY_TYPE_ENEMY);
			for each (_loc_2 in _loc_1)
			{
				
				if (!_loc_2.isDying() && _loc_2.isVisible())
				{
					return true;
				}
			}
			return false;
		}
		
		private static function _playAmbientMusic() : void
		{
			var _loc_1:* = RxAssetSound.GetByURL("sounds/ambient/ambient_forestday_generic_1.mp3");
			if (_loc_1 != null)
			{
				s_ambientChannel.Play(_loc_1, s_ambientVolume, 0, null, int.MAX_VALUE);
			}
		}
		private static function _playCombatMusic() : void
		{
			s_state = STATE_COMBAT;
			var _loc_1:* = RxAssetSound.GetByURL("sounds/music/valor_combat_v3.mp3");
			if (_loc_1 != null)
			{
				s_combatChannel.Play(_loc_1, s_combatVolume, 0, _onCombatComplete);
				s_mapChannel.SetVolume(s_mapVolume / 2);
			}
		}
		
		private static function _onCombatComplete(param1:RxSoundChannel) : void
		{
			s_state = STATE_MAP;
			s_mapChannel.SetVolume(s_mapVolume);
		}
		
		public static function SetMusicEnabled(param1:Boolean) : void
		{
			if (param1)
			{
				UnMute();
			}
			else
			{
				Mute();
			}
		}
		
		private static function UnMute() : void
		{
			if (s_musicEnabled || RxMain.IsAutomated())
			{
				return;
			}
			s_musicEnabled = true;
			_playAmbientMusic();
			_playMapMusic();
		}
		
		private static function _playMapMusic() : void
		{
			s_state = STATE_MAP;
			var _loc_1:* = RxAssetSound.GetByURL("sounds/music/valor_daytime_v3.mp3");
			if (_loc_1 != null)
			{
				s_mapChannel.Play(_loc_1, s_mapVolume, 0, null, int.MAX_VALUE);
			}
		}
		
		private static function Mute() : void
		{
			if (!s_musicEnabled)
			{
				return;
			}
			s_musicEnabled = false;
			if (s_stingerChannel)
			{
				s_stingerChannel.Stop();
			}
			if (s_mapChannel)
			{
				s_mapChannel.Stop();
			}
			if (s_ambientChannel)
			{
				s_ambientChannel.Stop();
			}
			if (s_combatChannel)
			{
				s_combatChannel.Stop();
			}
		}
		
		public static function Initialize() : void
		{
			s_state = STATE_IDLE;
			if (RxMain.IsAutomated())
			{
				s_musicEnabled = false;
				return;
			}
			s_musicVolume = RxClientVars.GetNumber("masterMusicVolume");
			s_stingerVolume = RxClientVars.GetNumber("stingerMusicVolume") * s_musicVolume;
			s_mapVolume = RxClientVars.GetNumber("mapMusicVolume") * s_musicVolume;
			s_ambientVolume = RxClientVars.GetNumber("ambientMusicVolume");
			s_combatVolume = RxClientVars.GetNumber("combatMusicVolume") * s_musicVolume;
			var _loc_1:* = new RxAssetSound();
			_loc_1.SetURL("sounds/music/valor_stinger_v2.mp3");
			_loc_1.Load();
			s_stingerChannel = new RxSoundChannel(true);
			s_mapChannel = new RxSoundChannel(true);
			s_ambientChannel = new RxSoundChannel(true);
			s_combatChannel = new RxSoundChannel(true);
			if (s_musicEnabled)
			{
				s_stingerChannel.Play(_loc_1, s_stingerVolume, 0, OnBootComplete);
			}
		}
		
		private static function OnBootComplete(param1:RxSoundChannel) : void
		{
			var _loc_2:* = RxAssetSound.GetByURL("sounds/music/valor_stinger_v2.mp3");
			_loc_2.Delete();
		}
		
		public static function LoadAll() : void
		{
			var _local1:XML;
			var _local2:RxAssetGroup;
			if (!s_musicLoaded)
			{
				_local1 = <assetGroup>
						<sound url="sounds/ambient/ambient_forestday_generic_1.mp3"/>
						<sound url="sounds/music/valor_combat_v3.mp3"/>
						<sound url="sounds/music/valor_daytime_v3.mp3"/>
					</assetGroup>;
				_local2 = new RxAssetGroup();
				_local2.Init(_local1, null);
				_local2.Load();
				s_musicLoaded = true;
			}
		}
		
		public static function MapReady() : void
		{
			if (!s_musicEnabled)
			{
				return;
			}
			
			_playAmbientMusic();
			_playMapMusic();
		}
	}
}